﻿$(function () {

});
var configs = {
    s_url:$.trim($(".shop_site_path").val()),
    api_key:$.trim($(".shop_api_key").val()),
    api_sgin:$.trim($(".shop_api_sgin").val()),
    user_id:$.trim($(".shop_user_id").val())
}

var orderdelHttpHelper={
    s_url:configs.s_url,
    params:'json={"api_key":"'+configs.api_key+'","user_id":'+configs.user_id+',"api_sgin":"'+configs.api_sgin+'","client_type":2,"ver":13,',
    deleteOrd:function(id){
        var params = orderdelHttpHelper.params+'"orderid":"'+id+'","service":"Grain.orderDelete"}';
        this.doPost(params,id);
    },

    doPost:function(params,id){
        var result = {};
        $.ajax({
            type:"post",
            url:this.s_url,
            dataType:'json',
            data:params,
            success:function Response(data,id){
                orderdelHttpHelper.callback(data,id);
            }
        });
        return result;
    },
    callback:function(obj,id){


            if(obj.code=200) {

                        window.location.reload();

            } else {
                layer.closeAll();
                layer.alert(obj.msg);
            }
        }

}


//取消订单
function cancelOrder(orderId) {

    //询问框
    layer.confirm('确定关闭？关闭后该订单将不显示在订单列表。', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        layer.closeAll();
        //加载层
        layer.load(0, { shade: [0.3, '#000'] });

        $.ajax({
            url: "../Order/OrderCancel",
            type: "post",
            data: { orderId: orderId },
            success: function (msg) {
                if(msg=="OK") {
                    layer.alert('订单已关闭', function () {
                        location.href = '../Order/OrderList';
                    });
                    $(".layui-layer-setwin a").click(function () {
                        location.href = '../Order/OrderList';
                    });
                } else {
                    layer.closeAll();
                    layer.alert(msg);
                }
            }
        });

    }, function () {
        layer.closeAll();
    });

}

//确认收货
function confirmOrder(orderId) {
    //询问框
    layer.confirm('确定收货？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        layer.closeAll();
        //加载层
        layer.load(0, { shade: [0.3, '#000'] });

        $.ajax({
            url: "../Order/OrderConfirm",
            type: "post",
            data: { orderId: orderId },
            success: function (msg) {
                if (msg == "OK") {
                    layer.alert('收货完成', function () {
                        location.href = '../Order/OrderList';
                    });
                    $(".layui-layer-setwin a").click(function () {
                        location.href = '../Order/OrderList';
                    });
                } else {
                    layer.closeAll();
                    layer.alert(msg);
                }
            }
        });

    }, function () {
        layer.closeAll();
    });
}

//再次购买
function orderReBuy(orderId) {
    //询问框
    layer.confirm('是否再一次购买该订单内所有商品？<br />确认后会将订单内所有商品全部加入购物车！', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        layer.closeAll();
        //加载层
        layer.load(0, { shade: [0.3, '#000'] });
        $.ajax({
            url: "../Order/OrderReBuy",
            type: "post",
            data: { orderId: orderId },
            success: function (msg) {
                if (msg == "OK") {
                    layer.closeAll();
                    clearCartCookie();
                    layer.msg('订单商品已加入购物车', { time: 1500 });
                    clearCartCookie();
                    loadCartCount();
                } else {
                    layer.closeAll();
                    layer.alert(msg);
                }
            }
        });
    }, function () {
        layer.closeAll();
    });

}

//导出
function orderExport(orderId) {
    //加载层
    layer.load(0, { shade: [0.3, '#000'] });
    $.ajax({
        url: "../Order/OrderExport",
        type: "post",
        data: { orderId: orderId },
        success: function (data) {
            layer.closeAll();
            exportExcel(data, orderId);
        }
    });
    
}

//前端导出
function exportExcel(data, orderId) {
    var rows = $.parseJSON(data);		
    var trtdCode = "<tr>";
    trtdCode += "<td style='height:24px'>积分促销</td>";
    trtdCode += "<td>商品编码</td>";
    trtdCode += "<td>商品名称</td>";
    trtdCode += "<td>单位</td>";
    trtdCode += "<td>包装数</td>";
    trtdCode += "<td>数量</td>";
    trtdCode += "<td>发货数量</td>";
    trtdCode += "<td>批发价</td>";
    trtdCode += "<td>平台费率</td>";
    trtdCode += "<td>平台费</td>";
    trtdCode += "<td>总数量</td>";
    trtdCode += "<td>金额</td>";
    trtdCode += "<td>门店积分</td>";
    trtdCode += "<td>单位</td>";
    trtdCode += "<td>国际条码</td>";
    trtdCode += "<td>运营分类</td>";
    trtdCode += "<td>备注</td>";
    trtdCode += "</tr>";

    for (var i = 0; i < rows.length; i++) {
        trtdCode += "<tr>";
        trtdCode += "<td style='height:20px'>" + rows[i].isPromotion + "</td>";
        trtdCode += "<td x:str=\"" + rows[i].SKU + "\">" + rows[i].SKU + "</td>";
        trtdCode += "<td>" + rows[i].ProductName + "</td>";
        trtdCode += "<td>" + rows[i].SaleUnit + "</td>";
        trtdCode += "<td style='mso-number-format:\"#,##0.00\";'>" + rows[i].SalePackingQty + "</td>";
        trtdCode += "<td style='mso-number-format:\"#,##0.00\";'>" + rows[i].PreQty + "</td>";
        trtdCode += "<td style='mso-number-format:\"#,##0.00\";'>" + rows[i].SaleQty + "</td>";
        trtdCode += "<td style='mso-number-format:\"#,##0.0000\";'>" + rows[i].SalePrice + "</td>";
        trtdCode += "<td style='mso-number-format:\"#,##0.000\";'>" + rows[i].ShopAddPerc + "</td>";
        trtdCode += "<td style='mso-number-format:\"#,##0.0000\";'>" + rows[i].SubAddAmt + "</td>";
        trtdCode += "<td style='mso-number-format:\"#,##0.00\";'>" + rows[i].SubQty + "</td>";
        trtdCode += "<td style='mso-number-format:\"#,##0.0000\";'>" + rows[i].SubAmt + "</td>";
        trtdCode += "<td style='mso-number-format:\"#,##0.00\";'>" + rows[i].SubPoint + "</td>";
        trtdCode += "<td>" + rows[i].Unit + "</td>";
        trtdCode += "<td x:str=\"" + rows[i].BarCode + "\">" + rows[i].BarCode + "</td>";
        trtdCode += "<td>" + rows[i].ShopCategoryNames + "</td>";
        trtdCode += "<td>" + rows[i].Remark + "</td>";
        
        trtdCode += "</tr>";
    }

    var dataCode = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>export</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table border="1">{table}</table></body></html>';
    dataCode = dataCode.replace("{table}", trtdCode);

    event.preventDefault();
    var bb = self.Blob;
    saveAs(
        new bb(
            ["\ufeff" + dataCode] //\ufeff防止utf8 bom防止中文乱码
            , { type: "html/plain;charset=utf8" }
        ), "订单导出_" + orderId + ".xls"
    );
    
}